<!DOCTYPE html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path;

%>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="IMG/favicon.ico">


    <title>任务规则配置</title>

    <!-- Bootstrap core CSS -->
    <link href="static/css/dist/css/bootstrap.min.css?v=1" rel="stylesheet">
    <!-- Bootstrap theme -->
    <link href="static/css/dist/css/bootstrap-theme.min.css" rel="stylesheet">
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="static/css/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="static/css/theme.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="static/css/loading.css" rel="stylesheet">

    <!--[if lt IE 9]>
    <script src="static/css/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="static/css/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>

    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>


    <![endif]-->
</head>

<body role="document">


<!-- Fixed navbar -->
<jsp:include page="top.jsp" flush="true"/>

<div class="container">

<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
      <span style="font-family:微软雅黑;">
      任务规则配置
        <button type="button" id="loadButton" name="loadButton" data-toggle="modal" data-target="#addModal"
                class="btn btn-default">
            新增

        </button>
           <span style="color:red">${msg}</span>
      </span>
</div>
<div class="panel-body">
    <p><span style="font-family:微软雅黑;">此页面配置发送任务，包括EMAIL分组选择、数据源选择等等。注意：任务名是唯一的</span></p>
</div>

<!--Model ALL-->
<!-- Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="addModalLabel">添加任务配置</h4>
            </div>
            <div class="modal-body" style="font-family:微软雅黑;">
                <form class="form-horizontal" action="<%= basePath %>/taskAdd" method="post"
                      enctype="multipart/form-data"
                      name="addData">
                    <div class="form-group">
                        <label for="taskName" class="col-sm-2 control-label">任务名</label>

                        <div class="col-sm-10">
                            <input name="taskName" type="text" class="form-control" id="taskName" placeholder="任务名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="cronExpression" class="col-sm-2 control-label">发送时间</label>

                        <div class="col-sm-10">
                            <input name="cronExpression" type="text" class="form-control" id="cronExpression"
                                   placeholder="发送时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="taskDesc" class="col-sm-2 control-label">任务描述</label>

                        <div class="col-sm-10">
                            <input name="taskDesc" type="text" class="form-control" id="taskDesc" placeholder="任务描述">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">执行方式</label>
                        &nbsp;&nbsp;
                        <label class="radio-inline">
                            <input type="radio" name="isAutoRun" value="1" checked> 自动执行
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="isAutoRun" value="0"> 手动执行
                        </label>
                    </div>

                    <div class="form-group">
                        <label for="dataSource" class="col-sm-2 control-label">数据源</label>

                        <div class="col-sm-10">
                            <select class="form-control" name="dataSource" id="dataSource">

                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="emailRule" class="col-sm-2 control-label">邮件分组</label>

                        <div class="col-sm-10">
                            <select class="form-control" name="emailRule" id="emailRule">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="ExcelFile" class="col-sm-2 control-label">上传表格</label>

                        <div class="col-sm-10">
                            <input type="file" id="ExcelFile" name="excelFile">
                        </div>

                    </div>
                    <div class="form-group">
                        <label for="sheetNames" class="col-sm-2 control-label">需要发送的sheet名称</label>

                        <div class="col-sm-10">
                            <input type="text" id="sheetNames" class="form-control" name="sheetNames"
                                   placeholder="填写EXCEL中的需要发送的sheet名称，如有多个Sheet，用*分割，如：Sheet1*Sheet2">
                        </div>
                    </div>
                    <%--<div class="form-group">--%>
                    <%--<label for="picturePix" class="col-sm-2 control-label">图片分辨率</label>--%>
                    <%--<div class="col-sm-10">--%>
                    <%--<input type="text" id="picturePix" class="form-control" name="picturePix" placeholder="100">--%>
                    <%--</div>--%>
                    <%--</div>--%>
                    <div class="form-group" align="right">
                        <button type="submit" id="addDateButton" name="addDateButton" class="btn btn-default">添加
                        </button>
                        &nbsp;&nbsp;&nbsp;
                    </div>
                </form>

            </div>

        </div>
    </div>
</div>


<div class="modal fade" id="lookModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="lookModalLabel">查看任务配置</h4>
            </div>
            <div class="modal-body" style="font-family:微软雅黑;">
                <form class="form-horizontal" action="#" method="post"
                      enctype="multipart/form-data"
                      name="addData">
                    <div class="form-group">
                        <span class="col-sm-3 " ><strong>任务名：</strong></span>

                        <div class="col-sm-9">
                            <span id="taskNameLook"/>
                        </div>

                    </div>

                    <div class="form-group">
                        <span class="col-sm-3 " ><strong>发送时间：</strong></span>
                        <div class="col-sm-9">
                            <span id="cronExpressionLook"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <span class="col-sm-3 " ><strong>任务描述：</strong></span>

                        <div class="col-sm-9">
                            <span id="taskDescLook"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <span class="col-sm-3" ><strong>数据源：</strong></span>
                        <div class="col-sm-9">
                            <span id="dataSourceLook"></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <span class="col-sm-3" ><strong>邮件分组：</strong></span>

                        <div class="col-sm-9">
                            <span id="emailRuleLook"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <span class="col-sm-3 " ><strong>上传表格：</strong></span>
                        <div class="col-sm-9">
                        <span id="excelFileLookLook"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <span class="col-sm-3 " ><strong>需要发送的sheet名称：</strong></span>

                        <div class="col-sm-9">
                            <span id="sheetNamesLook"></span>
                        </div>
                    </div>
                </form>

            </div>

        </div>
    </div>
</div>


<div class="modal fade" id="alterModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="alterModalLabel">修改任务规则</h4>
            </div>
            <div class="modal-body" style="font-family:微软雅黑;">
                <form class="form-horizontal" action="<%= basePath %>/taskAdd" method="post" name="addData">
                    <div class="form-group">
                        <label for="taskName" class="col-sm-2 control-label">任务名</label>

                        <div class="col-sm-10">
                            <input name="alterTaskName" type="email" class="form-control" id="alterTaskName"
                                   placeholder="任务名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="cronExpression" class="col-sm-2 control-label">发送规则</label>

                        <div class="col-sm-10">
                            <input name="alterCronExpression" type="text" class="form-control" id="alterCronExpression"
                                   placeholder="发送规则">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="taskDesc" class="col-sm-2 control-label">任务描述</label>

                        <div class="col-sm-10">
                            <input name="alterTaskDesc" type="text" class="form-control" id="alterTaskDesc"
                                   placeholder="任务描述">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">执行方式</label>
                        &nbsp;&nbsp;
                        <label class="radio-inline">
                            <input type="radio" name="alterIsAutoRun" id="alterIsAutoRun1" value="1" checked> 自动执行
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="alterIsAutoRun" id="alterIsAutoRun0" value="0"> 手动执行
                        </label>
                    </div>

                    <div class="form-group">
                        <label for="dataSource" class="col-sm-2 control-label">数据源</label>

                        <div class="col-sm-10">
                            <select class="form-control" name="alterDataSource" id="alterDataSource">
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="emailRule" class="col-sm-2 control-label">邮件规则</label>

                        <div class="col-sm-10">
                            <select class="form-control" name="alterEmailRule" id="alterEmailRule">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="ExcelFile" class="col-sm-2 control-label">上传表格</label>

                        <div class="col-sm-10">
                            <input type="file" id="alterExcelFile" name="alterExcelFile">
                        </div>

                    </div>

                    <div class="form-group" align="right">
                        <button type="submit" id="alterDateButton" name="alterDateButton" class="btn btn-default">修改
                        </button>
                        &nbsp;&nbsp;&nbsp;
                    </div>
                </form>

            </div>

        </div>
    </div>
</div>

<!-- Table -->
<table class="table" style="font-family:微软雅黑;">
    <tr>

        <th>序号</th>
        <th>任务名</th>
        <th>发送规则</th>
        <th>任务描述</th>
        <th>是否自动执行</th>
        <th>状态</th>
        <th>操作</th>

    </tr>
    <%int i = 0;%>
    <c:forEach items="${taskList}" var="list">
        <tr>

            <td><%=++i%>
            </td>
            <td>${list.taskAliasName}</td>
            <td>${list.cronExpression}</td>
            <td>${list.taskDesc}</td>
            <td>
                <c:if test="${list.isAutoRun==1}">自动</c:if>
                <c:if test="${list.isAutoRun==0}">手动</c:if>
            </td>
            <td>
                <c:if test="${list.taskStatus=='1'}">
                    执行中
                </c:if>
                <c:if test="${list.taskStatus=='0'}">
                    待执行
                </c:if>
                <c:if test="${list.taskStatus=='2'}">
                    暂停中
                </c:if>
                <c:if test="${list.taskStatus=='999'}">
                    异常
                </c:if>
            </td>

            <td>
                <c:if test="${list.taskStatus=='0'}">

                    <a href="<%= basePath %>/run?taskDetailId=${list.taskDetailModelId}">执行</a>|
                    <a href="#" onclick="runOnceNow('${list.taskDetailModelId}');">立即执行一次</a>|

                </c:if>
                <c:if test="${list.taskStatus=='1'}">

                    <a href="<%= basePath %>/pauseTask?taskDetailId=${list.taskDetailModelId}">暂停</a>|
                    <a href="#" onclick="runOnceNow('${list.taskDetailModelId}');">立即执行一次</a>|


                </c:if>
                <c:if test="${list.taskStatus=='2'}">

                    <a href="<%= basePath %>/resumeTask?taskDetailId=${list.taskDetailModelId}">恢复</a>|

                </c:if>
                <c:if test="${list.taskStatus=='999'}">
                    <a href="<%= basePath %>/run?taskDetailId=${list.taskDetailModelId}">重新执行</a>|
                </c:if>
                <a href="<%= basePath %>/taskDelete?taskDetailId=${list.taskDetailModelId}">删除</a>|<a
                    href="#" data-toggle="modal" data-target="#lookModal"
                    onclick="lookDetail('${list.taskDetailModelId}');">查看详情</a>
                    <%--<a href="#" data-toggle="modal" data-target="#alterModal" onclick="queryAlterTask('${list.taskDetailModelId}')">修改</a>--%>

            </td>
        </tr>
    </c:forEach>

</table>
</div>

</div>
<!-- /container -->


<jsp:include page="bottom.jsp" flush="true"/>


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->

<script src="static/js/jquery.min.js"></script>
<script src="static/js/jquery.json.min.js"></script>
<script src="static/css/dist/js/bootstrap.min.js"></script>
<script src="static/css/assets/js/docs.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="static/css/assets/js/ie10-viewport-bug-workaround.js"></script>
<script src="static/js/CommonPerson.js"></script>
<!--
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
-->
<script>
    $(document).ready(function () {
        /* 获取数据源 */
        $.post("loadDataBase", function (data) {

            var jsonObj = data;
            for (var i = 0; i < jsonObj.length; i++) {
                var $option = $("<option></option>");
                $option.attr("value", jsonObj[i][0]);
                $option.text(jsonObj[i][1]);
                $("#alterDataSource").append($option);
                $("#dataSource").append($option);
            }
        });
        $.post("loadEmail", function (data) {

            var jsonObj = data;
            for (var i = 0; i < jsonObj.length; i++) {
                var $option = $("<option></option>");
                $option.attr("value", jsonObj[i][0]);
                $option.text(jsonObj[i][1]);
                $("#alterEmailRule").append($option);
                $("#emailRule").append($option);
            }
        });

    });
    function runOnceNow(taskDetailModelId) {
        /* 获取数据源 */
//        $.get("runOnce?taskDetailId="+taskDetailModelId, function (data) {
//            alert(data);
//        });
//        var obj={};
//        obj["taskDetailId"]=taskDetailModelId;
        CommonPerson.Base.LoadingPic.FullScreenShow();
        ///return false;
        $.ajax({
            url: "<%= basePath %>/runOnce?taskDetailId=" + taskDetailModelId,
            type: "GET",
            contentType: 'application/json;charset=utf-8', //设置请求头信息
            dataType: "json",
            //data: JSON.stringify(customerArray),    //将Json对象序列化成Json字符串，JSON.stringify()原生态方法
            data: "{}",            //将Json对象序列化成Json字符串，toJSON()需要引用jquery.json.min.js
            success: function (data) {
                alert(JSON.stringify(data));
                CommonPerson.Base.LoadingPic.FullScreenHide();
            },
            error: function (res) {
                alert(res.responseText);
                CommonPerson.Base.LoadingPic.FullScreenHide();
            }
        });

    }

    function lookDetail(taskDetailModelId) {
        $.ajax({
            url: "<%= basePath %>/lookTaskDetail?taskDetailId=" + taskDetailModelId,
            type: "GET",
            contentType: 'application/json;charset=utf-8', //设置请求头信息
            dataType: "json",
            //data: JSON.stringify(customerArray),    //将Json对象序列化成Json字符串，JSON.stringify()原生态方法
            data: "{}",            //将Json对象序列化成Json字符串，toJSON()需要引用jquery.json.min.js
            success: function (data) {
                document.getElementById("excelFileLookLook").innerText= data.excel.templateName;
                document.getElementById("sheetNamesLook").innerText= data.picture.picSheetNames;
                document.getElementById("taskNameLook").innerText= data.taskDetail.taskAliasName;
                document.getElementById("cronExpressionLook").innerText= data.taskDetail.cronExpression;
                document.getElementById("taskDescLook").innerText= data.taskDetail.taskDesc;
                document.getElementById("dataSourceLook").innerText= data.dataSource.dataSourceName;
                document.getElementById("emailRuleLook").innerText= data.email.emailName;

               // alert(JSON.stringify(data));
            },
            error: function (res) {
                alert(res.responseText);
            }
        });

    }


</script>
<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<script>


    function queryAlterTask(taskDetailId) {
        //点击修改按钮，所做的操作
        //这个是点击修改链接。。。。的方法


        var dataObj = {};
        dataObj = {"taskDetailModelId": taskDetailId};

        jQuery.ajax({
            url: "<%= basePath %>/queryTaskInfo?taskDetailId=" + taskDetailId,
            type: "POST",
            contentType: 'application/json;charset=utf-8', //设置请求头信息
            dataType: "json",
            //data: JSON.stringify(customerArray),    //将Json对象序列化成Json字符串，JSON.stringify()原生态方法
            data: jQuery.toJSON(dataObj),            //将Json对象序列化成Json字符串，toJSON()需要引用jquery.json.min.js
            success: function (data) {

                var taskDetailModelId = JSON.parse(data.info).taskDetailModelId;
                var taskName = JSON.parse(data.info).taskName;
                var cronExpression = JSON.parse(data.info).cronExpression;
                var taskDesc = JSON.parse(data.info).taskDesc;
                var isAutoRun = JSON.parse(data.info).isAutoRun;
                var dataSourceModelId = JSON.parse(data.info).dataSourceModelId;
                var emailModelId = JSON.parse(data.info).emailModelId;

                document.getElementById("alterTaskName").value = taskName;
                document.getElementById("alterCronExpression").value = cronExpression;
                document.getElementById("alterTaskDesc").value = taskDesc;
                if (isAutoRun == 1) {
                    document.getElementById("alterIsAutoRun1").checked = true;
                } else {
                    document.getElementById("alterIsAutoRun0").checked = false;
                }
                var selectSource = document.getElementById("alterDataSource");
                for (var i = 0; i < selectSource.options.length; i++) {
                    if (selectSource.options[i].value == dataSourceModelId) {
                        selectSource.options[i].selected = true;
                        break;
                    }
                }

                var alterEmailRule = document.getElementById("alterEmailRule");
                for (var i = 0; i < alterEmailRule.options.length; i++) {
                    if (alterEmailRule.options[i].value == emailModelId) {
                        alterEmailRule.options[i].selected = true;
                        break;
                    }
                }

                jQuery("#content").html(JSON.stringify(data));
            },
            error: function (res) {
                alert(res.responseText);
            }
        });
    }


</script>
</body>
</html>
